package com.ruoyi.web.controller.pv;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pv.domain.PvNotice;
import com.ruoyi.pv.service.IPvNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通知公告Controller
 *
 * @author zzx
 * @date 2023-04-11
 */
@RestController
@RequestMapping("/pv/notice")
public class PvNoticeController extends BaseController {
    @Autowired
    private IPvNoticeService pvNoticeService;

    /**
     * 小程序-查询在当前时间范围内的所有公告
     */
    @GetMapping("/applet-list")
    public TableDataInfo appletList() {
        PvNotice pvNotice = PvNotice.builder().deptId(getDeptId()).startTime(new Date()).build();
        List<PvNotice> list = pvNoticeService.selectPvNoticeAppletList(pvNotice);
        return getDataTable(list);
    }

    /**
     * 查询通知公告列表
     */

    @GetMapping("/list")
    public TableDataInfo list(PvNotice pvNotice) {
        startPage();
        pvNotice.setDeptId(getDeptId());
        List<PvNotice> list = pvNoticeService.selectPvNoticeList(pvNotice);
        return getDataTable(list);
    }

    /**
     * 导出通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('pv:notice:export')")
    @Log(title = "通知公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PvNotice pvNotice) {
        pvNotice.setDeptId(getDeptId());
        List<PvNotice> list = pvNoticeService.selectPvNoticeList(pvNotice);
        ExcelUtil<PvNotice> util = new ExcelUtil<PvNotice>(PvNotice.class);
        util.exportExcel(response, list, "通知公告数据");
    }

    /**
     * 获取通知公告详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pvNoticeService.selectPvNoticeById(id));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('pv:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PvNotice pvNotice) {
        pvNotice.setDeptId(getDeptId());
        return toAjax(pvNoticeService.insertPvNotice(pvNotice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('pv:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PvNotice pvNotice) {
        return toAjax(pvNoticeService.updatePvNotice(pvNotice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('pv:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pvNoticeService.deletePvNoticeByIds(ids));
    }
}
